/*The MIT License

Copyright (c) <1/27/2011> <erick.difiore>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

package com.d4a5.mapping.google
{
	import com.d4a5.mapping.core.AmfPackages;
	import com.d4a5.mapping.core.IHighlightedObject;
	import com.d4a5.mapping.core.MapObjectStateContainer;
	import com.google.maps.Map;
	import com.google.maps.MapEvent;
	import com.google.maps.PaneId;
	import com.google.maps.interfaces.IOverlay;
	import com.google.maps.interfaces.IPane;
	import com.google.maps.interfaces.IPaneManager;
	
	import flash.events.MouseEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	
	import mx.collections.ArrayCollection;
	import mx.events.CollectionEvent;
	import mx.events.CollectionEventKind;
	import mx.events.ResizeEvent;
	
	public class wmgMap extends Map
	{
		protected var _baseMapsVisible:Boolean = true;
		protected var _baseMapsVisibleChanged:Boolean = false;
			
		protected var _layers:ArrayCollection = new ArrayCollection();
		protected var _layersChanged:Boolean = false;
		
		public function wmgMap()
		{
			super();
			
			this.addEventListener(MapEvent.MAP_READY,onMapReady);
			this.addEventListener(ResizeEvent.RESIZE,onMapResize);
			this._layers.addEventListener(CollectionEvent.COLLECTION_CHANGE,onLayersChange);
		}
		
		protected function onMapReady( event:MapEvent ) : void {
			this.validateLayers();
			this.removeEventListener(MapEvent.MAP_READY,onMapReady);
		}
		
		protected function onLayersChange( event:CollectionEvent ) : void {
			this._layersChanged = true;
			this.validateLayers();
		}
		
		protected function validateLayers() : void {
			if( this._layersChanged ){
				if( this.isLoaded() == false ){
					this.callLater( this.validateLayers );
				}
				else{
					this._layersChanged = false;
					
					var manager:IPaneManager = this.getPaneManager();
					
					var layersHash:Dictionary = new Dictionary();
					var layers:Array = this._layers.toArray();
					var i:int = layers.length;
					
					while( i-- ){
						if( layers[i] is IwmgLayer ){
							var layer:IwmgLayer = layers[i];
							if( layer.pane == null ){
								layer.pane = manager.createPane();
								//layer.pane = new wmgPane( this );
								manager.placePaneAt( layer.pane , PaneId.PANE_OVERLAYS+1 );
							}
							layersHash[ layer.pane ] = layer;
						}
						else{
							this._layers.removeItemAt( i );
						}
					}
					
					var length:int = manager.paneCount;
					for( i = 0; i < length; i++ ){
						var pane:IPane = manager.getPaneAt( i );
						if( !(layersHash[ pane ]) ){
							manager.removePane( pane );
						}
					}
				}
			}
		}
		
		public function get layers() : ArrayCollection {
			return this._layers;
		}
		
		public function set layers( newLayers:ArrayCollection ) : void {
			this._layers.removeAll();
			
			if( newLayers ){
				var nl:Array = newLayers.toArray();
				var numLayers:int = nl.length;
				for( var i:int = 0; i < numLayers; i++ ){
					this.addLayer( nl[i] as IwmgLayer );
				}
			}
		}
		
		
		public function addLayer( layer:IwmgLayer ) : void {
			layer.name = ( layer.name ) ? layer.name : "layer" + this._layers.length.toString();
			
			this._layers.addItem( layer );
		}
		protected function removeLayer( layer:IwmgLayer ) : void {
			var index:int = this._layers.getItemIndex( layer );
			
			if( index >= 0 ){
				this._layers.removeItemAt( index )
			}
		}
		
		public function getLayerByPane( pane:IPane ) : IwmgLayer {
			var layers:Array = this._layers.toArray();
			var length:int = layers.length;
			for( var i:int = 0; i < length; i++ ){
				if( layers[i].pane == pane ){
					return layers[i];
				}
			}
			return null;
		}
		
		protected function onMapResize( event:ResizeEvent ) : void {
			var layers:Array = this._layers.toArray();
			var length:int = layers.length;
			for( var i:int = 0; i < length; i++ ){
				layers[i].refresh();
			}
		}
		

		public function getAmfPackages() : AmfPackages {
			var packages:AmfPackages = new AmfPackages();
			
			var layers:Array = this._layers.toArray();
			var length:int = layers.length;
			for( var i:int = 0; i < length; i++ ){
				packages.add( layers[i].getAmfPackage( layers[i].name ) );
			}
			
			return packages;
		}
		
	}
}